package com.hns.can.mainte.web;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.vo.CommonParamsVo;
import com.hns.can.common.web.BaseController;
import com.hns.can.mainte.entity.AppCarExamReportEntity;
import com.hns.can.mainte.entity.CommMftCopaEntity;
import com.hns.can.mainte.service.ReportExaminReportService;
import com.hns.can.mainte.vo.ExamReportParamsVo;
import com.hns.can.mainte.vo.MaintenanceReportDetailVo;
import com.hns.can.mainte.vo.MaintenanceReportParamVo;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 车辆体检
 * @author:yecb
 * @email:lps00@163.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/reportExaminReport")
public class ReportExaminReportController extends BaseController  {
	
	final private String[] headerNames = new String[]{"车牌号","运营线路","上次体检时间","故障总数","未解决"};
	final private String[] headerFields = new String[]{"licPltNo", "lineName", "examTime", "mftTotal", "resolve"};

	
    @Autowired
    private ReportExaminReportService reportExaminReportService;

	/**
	 * 查询上次的体检结果
	 * @return
	 * @throws Exception
	 * @author:yecb
	 * @email:lps00@163.com
	 */
    @RequestMapping("/searchLastExaminReport")
    @ResponseBody
    public Object searchLastExaminReport(ExamReportParamsVo vo) throws Exception {
    	Pagination page=reportExaminReportService.searchLastExaminReport(vo);
    	GridPage grid = new GridPage(page);
        return getReturnResult(grid);
    }
    
	/**
	 * 手工修复故障
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
    @RequestMapping("/manualRepirFault")
    @ResponseBody
    public Object manualRepirFault(ExamReportParamsVo vo) throws Exception {
    	reportExaminReportService.manualRepirFault(vo);    	
        return getReturnResult("修复完成");
    }
    
    /**
	 * 查询上次的体检汇总结果
	 * @return
	 * @throws Exception
	 * @author:yecb
	 * @email:lps00@163.com
	 */
    @RequestMapping("/getLastAggExaminReport")
    @ResponseBody
    public Object getLastAggExaminReport() throws Exception {
    	List<AppCarExamReportEntity> list=reportExaminReportService.getLastAggExaminReport(getCurrentUser().getUser());
        return getReturnResult(list);
    }
    /**
     * 获取故障的分布
     * @param type 全部，已修复，未修复 查询这3种类别
     * @return
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/getMftNumList")
    @ResponseBody
    public Object getMftNumList(@RequestParam(value="type", required = false) String type) throws Exception {
    	List<CommMftCopaEntity> list=reportExaminReportService.getMftNumList(getCurrentUser().getUser(),type);
        return getReturnResult(list);
    }
    
    /**
     * 体检结果的车辆对应故障ECU节点查询
     * @param carId 车辆ID
     * @param exam_time 体检时间 ，这里需要获取当前体检时间的前一天
     * @return
     * @throws Exception
     * @author:yecb
     * @email:lps00@163.com
     */
    @RequestMapping("/getExaminReportDetail")
    @ResponseBody
    public Object getExaminReportDetail(@RequestParam("carId") String carId,@RequestParam("exam_time") String exam_time) throws Exception {
		MaintenanceReportParamVo conditionVo =new MaintenanceReportParamVo();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(sf.parse(exam_time));   //设置当前日期  
        c.add(Calendar.DATE, -1); //日期减1天,获取前一天数据
		conditionVo.setCarId(carId);
		conditionVo.setExam_time(sf.format(c.getTime()));
		List<MaintenanceReportDetailVo> list =reportExaminReportService.getExaminReportDetail(conditionVo);
        return getReturnResult(list);
    }
    
    @RequestMapping("/searchUnrepairAlmByCar")
    @ResponseBody
    public Object searchUnrepairAlmByCar(@RequestParam("carId") String carId) throws Exception {
		Map<String,String> map=new HashMap<String,String>();
		map.put("carId", carId);
		Pagination page = reportExaminReportService.searchUnrepairAlmByCar(map);
		GridPage grid = new GridPage(page);
        return getReturnResult(grid);
    }
    
    /**
	 * 体检按钮功能
	 * @param conditionVo
	 * @return
	 * @throws Exception
	 * @author:yecb
	 * @email:lps00@163.com
	 */
    @RequestMapping("/getSaveExaminReport")
    @ResponseBody
    public Object getSaveExaminReport(MaintenanceReportParamVo conditionVo) throws Exception {
    	SysUserEntity sysUserEntity=getCurrentUser().getUser();
    	conditionVo.setCorpId(sysUserEntity.getCorpId());
    	//1.查询出体检报告
    	List<AppCarExamReportEntity> list=reportExaminReportService.searchExaminReport(conditionVo,sysUserEntity);
    	//2.保存体检报告
    	if(PubUtil.isEmpty(list)){
    		throw new BusinessException(MsgType.WARN.getFlag(),"没有新的数据产生体检报告!");
    	}
    	reportExaminReportService.saveExaminReport(list);
        return this.getReturnResult();
    }
    
    /**
	 * 导出excel文档功能
	 * @param vo
	 * @param response
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
    @RequestMapping("/exportLastExaminReportExcel")
    public void exportLastExaminReportExcel(ExamReportParamsVo vo,HttpServletResponse response) throws Exception {
    	List<AppCarExamReportEntity> list=reportExaminReportService.getExamCarInfo(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "excel", "ExaminReport"+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "体检报告数据");
    }
  
    /**
	 * 导出pdf文档功能
	 * @param vo
	 * @param response
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
    @RequestMapping("/exportLastExaminReportPdf")
    public void exportLastExaminReportPdf(ExamReportParamsVo vo,HttpServletResponse response) throws Exception {
    	List<AppCarExamReportEntity> list=reportExaminReportService.getExamCarInfo(vo);
        ExportUtil.exportFiles(Arrays.asList(headerNames), Arrays.asList(headerFields), list, response, "pdf", "ExaminReport"+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "体检报告数据");
    }
}
